package de.hochschuletrier.bachelor.antfarm.entity;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;

import de.hochschuletrier.bachelor.antfarm.entity.ant.AntTask;
import de.hochschuletrier.bachelor.antfarm.util.AssetManager;
import de.hochschuletrier.bachelor.antfarm.util.assets.AssetImage;

public class Nest extends Entity {
	private float energy;
	private int scoutingAnts;
	private int collectingAnts;
	private ArrayList<Ant> ants;
	private ArrayList<Foodsource> knownFoodsources;

	public Nest(int x, int y, int amountOfAnts, float startEnergy) {
		super("Nest", x, y, AssetManager.getInstance().getImage(AssetImage.nest));

		ants = new ArrayList<Ant>();
		knownFoodsources = new ArrayList<Foodsource>();
		createAnts(amountOfAnts);
	}

	public void putFood(Foodpiece foodpiece, Ant ant) {
		this.energy += foodpiece.getEnergy();
		if (!knownFoodsources.contains(foodpiece.getSource())) {
			knownFoodsources.add(foodpiece.getSource());
		}
		foodpiece = null;
		ant.setImage(AssetManager.getInstance().getImage(AssetImage.ant));
		ant.setFoodpiece(null);
		
		alertListener();
	}

	public void createAnts(int amount) {
		for (int i = 0; i < amount; ++i) {
			createAnt();
		}
	}

	public void createAnt() {
		Ant newAnt = new Ant(this);
		ants.add(newAnt);
	}

	public float getEnergy() {
		return energy;
	}

	public void setEnergy(float energy) {
		this.energy = energy;
	}

	public AntTask getTaskForAnt() {
		int knownFoodpieces = 0;
		for (Foodsource s : knownFoodsources) {
			knownFoodpieces += s.getAmountOfFoodpieces();
		}
		if (knownFoodsources.size() == 0 || knownFoodpieces < ants.size()) {
			scoutingAnts++;
			return AntTask.scouting;
		}
		collectingAnts++;
		return AntTask.collecting;
	}

	@Override
	public void update(GameContainer gc, int delta) {
	}

	public int getScoutingAnts() {
		return scoutingAnts;
	}

	public void setScoutingAnts(int scoutingAnts) {
		this.scoutingAnts = scoutingAnts;
	}

	public int getCollectingAnts() {
		return collectingAnts;
	}

	public void setCollectingAnts(int collectingAnts) {
		this.collectingAnts = collectingAnts;
	}
	
	public ArrayList<Foodsource> getKnownFoodsources() {
		return knownFoodsources;
	}
}
